<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<div class="condition-table flex flex-col h-full gap-y-2">
    <div class="flex justify-between items-center">
        <div>Conditions</div>
        @if (!isDisabled) {
            <button mat-icon-button class="primary-icon-button" type="button" (click)="newConditionClicked()">
                <i class="fa fa-plus"></i>
            </button>
        }
    </div>
    @if (hasConditions()) {
        <div class="listing-table">
            <table mat-table [dataSource]="dataSource">
                <!-- Expression Column -->
                <ng-container matColumnDef="expression">
                    <th mat-header-cell *matHeaderCellDef>Expression</th>
                    <td mat-cell *matCellDef="let item">
                        <div
                            [id]="formatId(item)"
                            class="pointer min-h-[21px]"
                            cdkOverlayOrigin
                            #trigger="cdkOverlayOrigin"
                            (click)="openEditor(trigger, item, $event)">
                            <ng-container
                                *ngTemplateOutlet="
                                    isEmptyString(item.condition.expression) ? blank : nonBlank;
                                    context: { $implicit: item.condition.expression }
                                "></ng-container>
                            <ng-template #blank>
                                <div class="empty neutral-color">Empty string set</div>
                            </ng-template>
                            <ng-template #nonBlank let-expression>
                                <div class="flex justify-between items-center">
                                    <div class="truncate" [title]="expression">
                                        {{ expression }}
                                    </div>
                                    @if (hasExtraWhitespace(expression)) {
                                        <div
                                            class="fa fa-info-circle primary-color"
                                            nifiTooltip
                                            [tooltipComponentType]="TextTip"
                                            tooltipInputData="The specified value contains leading and/or trailing whitespace character(s). This could produce unexpected results if it was not intentional."></div>
                                    }
                                </div>
                            </ng-template>
                        </div>
                    </td>
                </ng-container>

                <!-- Actions Column -->
                <ng-container matColumnDef="actions">
                    <th mat-header-cell *matHeaderCellDef></th>
                    <td mat-cell *matCellDef="let item">
                        <div class="flex items-center justify-end">
                            <button
                                mat-icon-button
                                [disabled]="isDisabled"
                                type="button"
                                [matMenuTriggerFor]="actionMenu"
                                class="h-16 w-16 flex items-center justify-center icon global-menu">
                                <i class="fa fa-ellipsis-v"></i>
                            </button>
                            <mat-menu #actionMenu="matMenu" xPosition="before">
                                <button mat-menu-item (click)="deleteCondition(item)">
                                    <i class="fa fa-trash primary-color mr-2"></i>
                                    Delete
                                </button>
                            </mat-menu>
                        </div>
                    </td>
                </ng-container>

                <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                <tr
                    mat-row
                    *matRowDef="let row; let even = even; columns: displayedColumns"
                    (click)="selectCondition(row)"
                    [class.selected]="isSelected(row)"
                    [class.even]="even"></tr>
            </table>
            <ng-template
                cdkConnectedOverlay
                [cdkConnectedOverlayOrigin]="editorTrigger"
                [cdkConnectedOverlayPositions]="editorPositions"
                [cdkConnectedOverlayPush]="true"
                [cdkConnectedOverlayHasBackdrop]="true"
                [cdkConnectedOverlayBackdropClass]="'cdk-overlay-transparent-backdrop'"
                [cdkConnectedOverlayOpen]="editorOpen"
                (detach)="closeEditor()">
                <ua-editor
                    [supportsEl]="true"
                    [required]="true"
                    [value]="editorItem.condition.expression"
                    [width]="editorWidth"
                    [readonly]="isDisabled"
                    (ok)="saveConditionValue(editorItem, $event)"
                    (exit)="closeEditor()"></ua-editor>
            </ng-template>
        </div>
    }
</div>
